#include <iostream>
#include <cstring>
using namespace std;

const int N = 1010;

int n;
int a[N];
int tr[N];
int x;

int lowbit(int x)
{
    return x & -x;
}

int find_p(int x) //找到x的父节点
{
    return x + lowbit(x);
}

void find_s(int p) //列举出p的全部构成
{
    cout << "a[" << p << "] "; //因为tr[x]必定要加a[x]
    for (int i = p - 1; lowbit(i) < lowbit(p) && i > 0; i -= lowbit(i))
        cout << "tr[" << i << "] ";
    return;
}

void init_by_p() //从自己加上去的初始化算法
{
    for (int i = 1; i <= n; ++i)
    {
        tr[i] += a[i];         //因为前面的子节点都自己加上来了，直接加上自己就是完整的了
        int p = i + lowbit(i); //然后找到自己的父节点
        if (p <= n)            //如果父节点还没出界
            tr[p] += tr[i];    //那就加上去
    }
    return;
}

void init_by_s() //找到后再加上去
{
    for (int i = 1; i <= n; ++i)
    {
        tr[i] = a[i]; //我至少有自己
        for (int j = i - 1; j > 0 && lowbit(j) < lowbit(i); j -= lowbit(j))
            tr[i] += tr[j];
    }
    return;
}

int sum(int x) //求前缀和
{
    int sum = 0;
    for (int i = x; i > 0; i -= lowbit(i))
        sum += tr[i];
    return sum;
}

void change(int i, int x) //把a[i]修改为x
{
    int k = x - a[i]; //增量
    a[i] = x;
    for (int j = i; j <= n; j += lowbit(j)) //我先修改我自己，然后再找到受我直接影响的父节点进行修改，然后父节点再处理他的父节点，直到边界
        tr[j] += k;                         //都要加上这个增量
    return;
}

int main()
{
    freopen("cin.txt", "r", stdin);
    cin >> n;
    memset(tr, 0, sizeof tr);
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    init_by_s(); //初始化
    cin >> x;
    cout << sum(x) << endl;
    change(1, 0);
    cout << sum(x) << endl;
    return 0;
}